//robot.c


//c runtime lib
#include <stdio.h>
//#include <stdlib.h>
//#include <malloc.h>
//#include <memory.h>
//#include <tchar.h>

#include "..\..\RobDeviceInterface\c\soapH.h"
#include "..\..\RobDeviceInterface\c\RobDeviceInterface.nsmap"


#ifdef __cplusplus
extern "C" {
#endif

int http_get(struct soap *pSoap)
{
	//soap_response(soap, SOAP_HTML); // HTTP response header with text/html
	//soap_send(soap, "<HTML>My Web server is operational.</HTML>");
	//soap_end_send(soap);
	//return SOAP_ERR;

	FILE* fd = fopen("RobDeviceInterface.wsdl", "rb"); // open WSDL file to copy
	if (!fd)
		return 404; // return HTTP not found error
	pSoap->http_content = "text/xml"; // HTTP header with text/xml content
	soap_response(pSoap, SOAP_FILE);
	for (;;)
	{	
		size_t r = fread(pSoap->tmpbuf, 1, sizeof(pSoap->tmpbuf), fd);
		if (!r)
			break;
		if (soap_send_raw(pSoap, pSoap->tmpbuf, r))
			break; // can't send, but little we can do about that
	}
	fclose(fd);
	soap_end_send(pSoap);
	soap_receiver_fault(pSoap, "Service method not implemented.", "");
	return SOAP_ERR;
}

int main(int argc, char **argv)
{ 
	SOAP_SOCKET m, s; /* master and slave sockets */

	//create soap
	struct soap mySoap;
	soap_init(&mySoap);

	//enable HTTP get operation
	mySoap.fget = http_get;

	//create socket
	m = soap_bind(&mySoap, NULL, 8080, 100);
	if (!soap_valid_socket(m))
	{ 
		soap_print_fault(&mySoap, stderr);
		exit(-1);
	}
	fprintf(stderr, "Socket connection successful: master socket = %d\n", m);

	//working
	for ( ; ; )
	{ 
		s = soap_accept(&mySoap);
		fprintf(stderr, "Socket connection successful: slave socket = %d\n", s);
		if (!soap_valid_socket(s))
		{ 
			soap_print_fault(&mySoap, stderr);
			exit(-1);
		} 
		soap_serve(&mySoap);
	}

	//close soap
	soap_end(&mySoap);

	return 0;
} 


//common
int ns1__Device_GetHardwareID(struct soap *pSoap, int deviceType, int deviceIndex, int *retHardwareID)
{ 
	*retHardwareID = 10;
	printf("ns1__Device_GetHardwareID %f.\n", *retHardwareID);
	return SOAP_OK;
} 

int ns1__Device_GetName(struct soap *pSoap,int deviceType, int deviceIndex, char **retName)
{ 
	*retName = "MyDevice";

	printf("ns1__Device_GetName %s.\n", *retName);
	return SOAP_OK;
} 

int ns1__Device_GetPose(struct soap *pSoap,int deviceType, int deviceIndex, struct ns1__Device_GetPoseResponse *retPose)
{ 

	retPose->Data[0] = 0;
	retPose->Data[1] = 1;
	retPose->Data[2] = 2;
	retPose->Data[3] = 3;
	retPose->Data[4] = 4;
	retPose->Data[5] = 5;
	retPose->Data[6] = 6;

	printf("ns1__Device_GetPose %f %f %f %f %f %f %f.\n"
	, retPose->Data[0]
	, retPose->Data[1]
	, retPose->Data[2]
	, retPose->Data[3]
	, retPose->Data[4]
	, retPose->Data[5]
	, retPose->Data[6]);
	return SOAP_OK;
} 

//DeviceDictionary
int ns1__DeviceDictionary_GetDeviceNumber(struct soap *pSoap,  int deviceIndex, int *retDeviceNumber)
{ 

	soap_receiver_fault(pSoap, "Service method not implemented.", "");
	return SOAP_ERR;
} 

int ns1__DeviceDictionary_GetNextDevice(struct soap *pSoap,  int deviceIndex,  int currentDeviceType,  int currentDeviceIndex, struct ns1__DeviceDictionary_GetNextDeviceResponse *retNextDevice)
{ 

	soap_receiver_fault(pSoap, "Service method not implemented.", "");
	return SOAP_ERR;
} 



//Stream



//AnalogSensor
int ns1__AnalogSensor_GetNormalizedMeasurement(struct soap *pSoap,  int deviceIndex, double *retValue)
{ 
	*retValue = 0.5;

	printf("ns1__AnalogSensor_GetNormalizedMeasurement %f.\n", *retValue);
	return SOAP_OK;
} 

int ns1__AnalogSensor_GetRawMeasurement(struct soap *pSoap,  int deviceIndex, double *retValue)
{ 

	*retValue = 0.5;

	printf("ns1__AnalogSensor_GetRawMeasurement %f.\n", *retValue);
	return SOAP_OK;
} 

int ns1__AnalogSensor_GetRawMeasurementRange(struct soap *pSoap,  int deviceIndex, double *retValue)
{ 

	*retValue = 1;
	printf("ns1__AnalogSensor_GetRawMeasurementRange %f.\n", *retValue);
	return SOAP_OK;
} 

int ns1__AnalogSensor_GetLastTimeStamp(struct soap *pSoap,  int deviceIndex, time_t *retValue)
{ 
	*retValue = time(NULL);

	printf("ns1__AnalogSensor_GetTimeStamp %s.\n", ctime(retValue));
	return SOAP_OK;
} 



//ContactSensor
int ns1__ContactSensor_IsPressed(struct soap *pSoap,  int deviceIndex, enum xsd__boolean  *retIsPressed)
{ 

	soap_receiver_fault(pSoap, "Service method not implemented.", "");
	return SOAP_ERR;
} 

int ns1__ContactSensor_GetLastTimeStamp(struct soap *pSoap,  int deviceIndex, time_t *retValue)
{ 

	soap_receiver_fault(pSoap, "Service method not implemented.", "");
	return SOAP_ERR;
} 



//Battery
int ns1__Battery_GetMaxBatteryPower(struct soap *pSoap,  int deviceIndex, double *retMaxPower)
{ 

	soap_receiver_fault(pSoap, "Service method not implemented.", "");
	return SOAP_ERR;
} 

int ns1__Battery_GetPercentBatteryPower(struct soap *pSoap,  int deviceIndex, double *retPercentPower)
{ 

	soap_receiver_fault(pSoap, "Service method not implemented.", "");
	return SOAP_ERR;
} 

int ns1__Battery_GetPercentCriticalBattery(struct soap *pSoap,  int deviceIndex, double *retPercentPower)
{ 

	soap_receiver_fault(pSoap, "Service method not implemented.", "");
	return SOAP_ERR;
} 

int ns1__Battery_SetPercentCriticalBattery(struct soap *pSoap,  int deviceIndex,  double critcalPercent, struct ns1__EmptyStruct *retEmpty)
{ 

	soap_receiver_fault(pSoap, "Service method not implemented.", "");
	return SOAP_ERR;
} 



//Motor
int ns1__Motor_GetCurrentMotorPower(struct soap *pSoap,  int deviceIndex, double *retPower)
{ 

	soap_receiver_fault(pSoap, "Service method not implemented.", "");
	return SOAP_ERR;
} 

int ns1__Motor_GetCurrentMotorPos(struct soap *pSoap,  int deviceIndex, double *retPose)
{ 

	soap_receiver_fault(pSoap, "Service method not implemented.", "");
	return SOAP_ERR;
} 

int ns1__Motor_GetCurrentMotorSpeed(struct soap *pSoap,  int deviceIndex, double *retSpeed)
{ 

	soap_receiver_fault(pSoap, "Service method not implemented.", "");
	return SOAP_ERR;
} 

int ns1__Motor_GetGearRatio(struct soap *pSoap,  int deviceIndex, double *retGearRatio)
{ 

	soap_receiver_fault(pSoap, "Service method not implemented.", "");
	return SOAP_ERR;
} 

int ns1__Motor_GetWheelRadius(struct soap *pSoap,  int deviceIndex, double *retWheelRadius)
{ 

	soap_receiver_fault(pSoap, "Service method not implemented.", "");
	return SOAP_ERR;
} 

int ns1__Motor_SetTargetMotorPower(struct soap *pSoap,  int deviceIndex,  double power, struct ns1__EmptyStruct *retEmpty)
{ 

	soap_receiver_fault(pSoap, "Service method not implemented.", "");
	return SOAP_ERR;
} 

int ns1__Motor_SetTargetMotorPos(struct soap *pSoap,  int deviceIndex,  double pose, struct ns1__EmptyStruct *retEmpty)
{ 

	soap_receiver_fault(pSoap, "Service method not implemented.", "");
	return SOAP_ERR;
} 

int ns1__Motor_SetTargetMotorSpeed(struct soap *pSoap,  int deviceIndex,  double speed, struct ns1__EmptyStruct *retEmpty)
{ 

	soap_receiver_fault(pSoap, "Service method not implemented.", "");
	return SOAP_ERR;
} 

int ns1__Motor_SetTarget(struct soap *pSoap,  int deviceIndex,  double pose,  double speed,  double refPower, struct ns1__EmptyStruct *retEmpty)
{ 

	soap_receiver_fault(pSoap, "Service method not implemented.", "");
	return SOAP_ERR;
} 

int ns1__Motor_Stop(struct soap *pSoap,  int deviceIndex, struct ns1__EmptyStruct *retEmpty)
{ 

	soap_receiver_fault(pSoap, "Service method not implemented.", "");
	return SOAP_ERR;
} 



//Encoder
int ns1__Encoder_GetCurrentAngle(struct soap *pSoap,  int deviceIndex, double *retAngle)
{ 

	soap_receiver_fault(pSoap, "Service method not implemented.", "");
	return SOAP_ERR;
} 

int ns1__Encoder_GetCurrentReading(struct soap *pSoap,  int deviceIndex, int *retReading)
{ 

	soap_receiver_fault(pSoap, "Service method not implemented.", "");
	return SOAP_ERR;
} 

int ns1__Encoder_GetTicksPerRevolution(struct soap *pSoap,  int deviceIndex, int *retTicksPerRev)
{ 

	soap_receiver_fault(pSoap, "Service method not implemented.", "");
	return SOAP_ERR;
} 

int ns1__Encoder_GetTicksSinceReset(struct soap *pSoap,  int deviceIndex, int *retTicksSinceReset)
{ 

	soap_receiver_fault(pSoap, "Service method not implemented.", "");
	return SOAP_ERR;
} 

int ns1__Encoder_ResetTickCounter(struct soap *pSoap,  int deviceIndex, struct ns1__EmptyStruct *retEmpty)
{ 

	soap_receiver_fault(pSoap, "Service method not implemented.", "");
	return SOAP_ERR;
} 

int ns1__Encoder_GetLastTimeStamp(struct soap *pSoap,  int deviceIndex, time_t *retValue)
{ 

	soap_receiver_fault(pSoap, "Service method not implemented.", "");
	return SOAP_ERR;
} 



//Sonar
int ns1__Sonar_GetAngularRange(struct soap *pSoap,  int deviceIndex, double *retAnglarRange)
{ 

	soap_receiver_fault(pSoap, "Service method not implemented.", "");
	return SOAP_ERR;
} 

int ns1__Sonar_GetAngularResolution(struct soap *pSoap,  int deviceIndex, double *retAnglarResolution)
{ 

	soap_receiver_fault(pSoap, "Service method not implemented.", "");
	return SOAP_ERR;
} 

int ns1__Sonar_GetMaxDistance(struct soap *pSoap,  int deviceIndex, double *retMaxDistance)
{ 

	soap_receiver_fault(pSoap, "Service method not implemented.", "");
	return SOAP_ERR;
} 

int ns1__Sonar_GetDistanceMeasurement(struct soap *pSoap,  int deviceIndex, double *retDistance)
{ 

	soap_receiver_fault(pSoap, "Service method not implemented.", "");
	return SOAP_ERR;
} 

int ns1__Sonar_GetLastTimeStamp(struct soap *pSoap,  int deviceIndex, time_t *retValue)
{ 

	soap_receiver_fault(pSoap, "Service method not implemented.", "");
	return SOAP_ERR;
} 



//Camera
int ns1__Camera_GetImageSize(struct soap *pSoap,  int deviceIndex, struct ns1__Camera_GetImageSizeResponse *retImageSize)
{ 
	retImageSize->Data[0] = 128;
	retImageSize->Data[1] = 128;

	printf("ns1__Camera_GetImageSize %f %f %f %f %f %f %f.\n"
	, retImageSize->Data[0]
	, retImageSize->Data[1]);
	return SOAP_OK;
} 

int ns1__Camera_GetViewAngle(struct soap *pSoap,  int deviceIndex, double *retViewAngle)
{ 
	*retViewAngle = 1;

	printf("ns1__Camera_GetViewAngle %l.\n", retViewAngle);
	return SOAP_OK;
} 

int ns1__Camera_GetQuality(struct soap *pSoap,  int deviceIndex, double *retQuality)
{ 
	*retQuality = 32;

	printf("ns1__Camera_GetQuality %l.\n", retQuality);
	return SOAP_OK;
} 

int ns1__Camera_GetLastFrame(struct soap *pSoap,  int deviceIndex, struct ns1__Camera_GetLastFrameResponse *retLastFrame)
{ 
	FILE* pFile;
	long size;
	int result;

	pFile = fopen("D:\\Transformer\\autograbbingrobot\\SourceCode\\RobotPlant\\Debug\\read.bmp","rb");
	fseek(pFile , 0 , SEEK_END);
	size = ftell(pFile);
	rewind(pFile);
	retLastFrame->Data.ImageData.__size = size;
	retLastFrame->Data.ImageData.__ptr = (unsigned char*)soap_malloc(pSoap, retLastFrame->Data.ImageData.__size);
	result = fread(retLastFrame->Data.ImageData.__ptr,1,retLastFrame->Data.ImageData.__size,pFile);
	fclose(pFile);

	retLastFrame->Data.TimeStamp = time(NULL);
	retLastFrame->Data.Width = 128;
	retLastFrame->Data.Height = 128;
	retLastFrame->Data.Format = ROBDEVICE_CAMERA_FORMAT_RGB888;
	retLastFrame->Data.Compression = ROBDEVICE_CAMERA_COMPRESS_RAW;

	printf("ns1__Camera_GetLastFrame read.bmp.\n");
	return SOAP_OK;
} 

int ns1__Camera_GetLastTimeStamp(struct soap *pSoap,  int deviceIndex, time_t *retValue)
{ 
	*retValue = time(NULL);

	printf("ns1__Camera_GetTimeStamp %s.\n", ctime(retValue));
	return SOAP_OK;
} 



//SickLRF



//Arm
int ns1__Arm_GetCurrentJointPose(struct soap *pSoap,  int deviceIndex, struct DoubleArray *retCurrentJointPose)
{ 

	soap_receiver_fault(pSoap, "Service method not implemented.", "");
	return SOAP_ERR;
} 

int ns1__Arm_GetCurrentJointVel(struct soap *pSoap,  int deviceIndex, struct DoubleArray *retCurrentJointVel)
{ 

	soap_receiver_fault(pSoap, "Service method not implemented.", "");
	return SOAP_ERR;
} 

int ns1__Arm_MoveJoint(struct soap *pSoap,  int deviceIndex, struct DoubleArray toJointValues, double speed, double flyByZone, struct ns1__EmptyStruct *retEmpty)
{ 

	soap_receiver_fault(pSoap, "Service method not implemented.", "");
	return SOAP_ERR;
} 

int ns1__Arm_Stop(struct soap *pSoap,  int deviceIndex,  enum xsd__boolean  followPath, struct ns1__EmptyStruct *retEmpty)
{ 

	soap_receiver_fault(pSoap, "Service method not implemented.", "");
	return SOAP_ERR;
} 



//DifferentialDrive
int ns1__DifferentialDrive_GetCurrentMoveSpeed(struct soap *pSoap,  int deviceIndex, double *retMoveSpeed)
{ 

	soap_receiver_fault(pSoap, "Service method not implemented.", "");
	return SOAP_ERR;
} 

int ns1__DifferentialDrive_GetCurrentTurnSpeed(struct soap *pSoap,  int deviceIndex, double *retTurnSpeed)
{ 

	soap_receiver_fault(pSoap, "Service method not implemented.", "");
	return SOAP_ERR;
} 

int ns1__DifferentialDrive_SetMoveSpeed(struct soap *pSoap,  int deviceIndex,  double moveSpeed, struct ns1__EmptyStruct *retEmpty)
{ 

	soap_receiver_fault(pSoap, "Service method not implemented.", "");
	return SOAP_ERR;
} 

int ns1__DifferentialDrive_SetTurnSpeed(struct soap *pSoap,  int deviceIndex,  double turnSpeed, struct ns1__EmptyStruct *retEmpty)
{ 

	soap_receiver_fault(pSoap, "Service method not implemented.", "");
	return SOAP_ERR;
} 

int ns1__DifferentialDrive_Stop(struct soap *pSoap,  int deviceIndex,struct ns1__EmptyStruct *retEmpty)
{ 

	soap_receiver_fault(pSoap, "Service method not implemented.", "");
	return SOAP_ERR;
} 

#ifdef __cplusplus
}
#endif